<template>
  <div>
    <el-dialog :title="title" :visible.sync="dialogVisible" width="50%" top="5vh" @close.native="cancel" :close-on-click-modal="false">
      <el-form ref="ruleForm" :model="form" :rules="rules" label-width="140px">
        <el-form-item prop="unitName">
          <template slot="label">
            <span>管理单位名称</span>
            <el-tooltip class="item" effect="dark" content="管理单位的正式完整名称，不可简写" placement="top">
              <i class="el-icon-question" style="color:#2c68ff" />
            </el-tooltip>
            <span>:</span>
          </template>
          <el-input v-model="form.unitName" placeholder="请输入" :disabled="isLook=='look'" />
        </el-form-item>
        <el-form-item label="所属大单位：" prop="largeName">
          <el-input v-model="form.largeName" placeholder="请输入" :disabled="isLook=='look'" />
        </el-form-item>
        <el-form-item label="评价考核年度：" prop="year">
          <el-date-picker style="width:100%" v-model="form.year" type="year" value-format="yyyy" placeholder="选择年度" :disabled="isLook=='look'">
          </el-date-picker>
        </el-form-item>
        <el-form-item prop="openRate">
          <template slot="label">
            <span>开放率（%）</span>
            <el-tooltip class="item" effect="dark" content="纳入共享的设施仪器数量/符合共享条件的设施仪器数量" placement="top">
              <i class="el-icon-question" style="color:#2c68ff" />
            </el-tooltip>
            <span>:</span>
          </template>
          <el-input v-model="form.openRate" placeholder="请输入" :disabled="isLook=='look'" />
        </el-form-item>
        <el-form-item label="年均机时：" prop="yearHours">
          <el-input v-model="form.yearHours" placeholder="请输入" :disabled="isLook=='look'" />
        </el-form-item>
        <el-form-item prop="sharingRate">
          <template slot="label">
            <span>共享率（%）</span>
            <el-tooltip class="item" effect="dark" content="年度平均共享服务机时/年度平台有效工作机时" placement="top">
              <i class="el-icon-question" style="color:#2c68ff" />
            </el-tooltip>
            <span>:</span>
          </template>
          <el-input v-model="form.sharingRate" placeholder="请输入" :disabled="isLook=='look'" />
        </el-form-item>
        <el-form-item label="服务收入（万元）：" prop="serviceRevenue">
          <el-input v-model="form.serviceRevenue" placeholder="请输入" type="textarea" :rows="4" :disabled="isLook=='look'" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="isLook!=='look'">确 定</el-button>
        <el-button @click="
          cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { uploadFile } from '@/api/common.js'
import {
  insertArticle,
  editArticle
  // articleDetail
} from '@/api/publishManage.js'
export default {
  data() {
    return {
      dialogVisible: false,
      iconClasses: ['icon-rate-face-1', 'icon-rate-face-2', 'icon-rate-face-3'],
      isLook: '',
      accept: '*',
      title: '',
      form: {
        unitName: '',
        largeName: '',
        year: '',
        openRate: '',
        sharingRate: '',
        serviceRevenue: '',
        yearHours: ''
      },
      facilityType: ['政策法规', '管理制度'],
      file: null,
      rules: {
        unitName: [
          { required: true, message: '不能为空！', trigger: 'blur' }
        ],
        largeName: [{ required: true, message: '不能为空！', trigger: 'blur' }],
        year: [
          { required: true, message: '不能为空！', trigger: 'blur' }
        ],
        openRate: [
          { required: true, message: '不能为空！', trigger: 'blur' }
        ],
        sharingRate: [
          { required: true, message: '不能为空！', trigger: 'blur' }
        ],
        serviceRevenue: [
          { required: true, message: '不能为空！', trigger: 'blur' }
        ],
        yearHours: [
          { required: true, message: '不能为空！', trigger: 'blur' }
        ],
      }
    }
  },
  created() { },
  methods: {
    show(obj) {
      this.dialogVisible = true
      this.title = obj.title
      this.isLook = obj.isLook
      if (obj.data) {
        this.form = Object.assign(obj.data)
        // articleDetail({ articleId: obj.data.articleId }).then(res => {
        //   this.form = res.data
        // })
      }
    },
    submitForm() {
      this.$refs['ruleForm'].validate(valid => {
        if (valid) {
          if (this.file) {
            const formData = new FormData()
            formData.append('file', this.file)
            formData.append('pathType', this.file.name.split('.').pop())
            uploadFile(formData).then(res => {
              this.form.fileId = res.data.fileId
              if (this.form.articleId) {
                editArticle(this.form).then(() => {
                  this.cancel()
                })
              } else {
                insertArticle(this.form).then(() => {
                  this.cancel()
                })
              }
            })
          } else {
            if (this.form.articleId) {
              editArticle(this.form).then(() => {
                this.cancel()
              })
            } else {
              insertArticle(this.form).then(() => {
                this.cancel()
              })
            }
          }
        } else {
          return false
        }
      })
    },
    cancel() {
      this.form = this.$options.data().form
      this.$refs.ruleForm.clearValidate()
      this.dialogVisible = false
      this.isLook = false
      this.file = null
      this.$emit('close')
    },
    handleChange(file) {
      this.file = file.raw
      this.form.fileName = file.name
    },
    // 多次上传覆盖
    handleExceed(file) {
      console.log('[ file ] >', file)
      this.$refs.uploadRef.clearFiles()
      // this.handleChange(file[0])
      this.file = file[0]
      this.form.fileName = file[0].name
    },
    downloadLink(item) {
      this.downloadByUrl(item.fileName, item.filePath)
    },
    closeFile() {
      this.file = null
      this.$refs.uploadRef.clearFiles()
    }
  }
}
</script>

<style lang="less" scoped>
  :deep(.el-table .cell) {
    font-size: 16px;
  }
  :deep(.el-table th > .cell) {
    font-size: 18px;
  }
</style>
